import store from '@/store.js'
import { getUserMe } from '@/api'
import { formatTimeToStr } from '@/utils/date'
import { goLogin, formatDate } from '@/utils'
import storage from '@/assets/utils/Storage'
const Storage = storage.storage;

export default {
	store,
	computed: {
		openId() {
			return store.state.index.openId
		},
		token() {
			let token =  Storage.getStorage('token')
			store.dispatch('setToken', {token})
			return store.state.index.token
		},
		userInfo() {
			return store.state.index.userInfo
		}
	},
	filters: {
		formatDate(time){
			console.log('time: ', time)
			if (time !== null && time !== '') {
				var date = new Date(time)
				// console.log('formatTimeToStr: ', formatTimeToStr(date, 'yyyy-MM-dd'))
				return formatTimeToStr(date, 'yyyy-MM-dd')
			  } else {
				return ''
			  }
		},
		formatDateYM(time){
			if (time !== null && time !== '') {
				var date = new Date(time)
				return formatTimeToStr(date, 'yyyy.MM')
			  } else {
				return ''
			  }
		},
		orderPrice(val,isNegotiable){
		   if(isNegotiable){
			   return "面议"
		   }else{
			   return '￥' + val;
		   }
		},
		formatMoney (val){
			if (val <= 0 ) return '0';
			if(val){
				val = (val /100).toFixed(2);
			}else{
				return ''
			}
			var st1=String(val);      // 将浮点数转换为字符串
			var index=st1.indexOf('.');    // 取小数点下标
		   var st2=st1.slice(0,index);	  // 将小数点前面的字符串截取出来
			var len=st2.length;
			if(len<3) return st2.concat(st1.slice(index));
			var r=len%3;
		   return  (r>0?
			   st2.slice(0,r)+','+st2.slice(r,len).match(/\d{3}/g).join():
				  st2.slice(r,len).match(/\d{3}/g).join())
			   .concat(st1.slice(index));
	   },
	},
	methods: {
		goLogin,
		formatDate: formatDate,
		bannerTolink(item){
			this.$router.open(item);
		},
		getUser(callback) {
			getUserMe().then(res => {
				if (res.code === 0) {
					this.$store.dispatch('setUser', {
						userInfo: {
							...res.data
						}
					})
				}
				if(callback){
					callback()
				}
			}).catch((res)=>{
				uni.showToast({
					title: res.msg,
					icon: 'none',
				});
			})
		},
		makePhoneCall(phoneNumber) {
			uni.makePhoneCall({
				phoneNumber: phoneNumber
			})
		},
		previewImage(img, arr) {
			let array = arr || [img]
			uni.previewImage({
				current: img,
				urls: array
			})
		},
		copy(data){
			uni.setClipboardData({
			  data,
			  success() {
				uni.showToast({
				  title: '复制成功',
				  icon: 'success',
				  duration: 1000
				})
			  }
			})
		},
	},
}
